# -*- coding: utf-8 -*-

 

'''
@author: José Gorgulho 5622
@date: 10/29/2012
@obs
Module containing class for interacting with excell xls files.
Inicially intended only for a Programming Languages Class work
'''

import xlrd
import unicodedata

class xls_handler():
    ''' Class for interacting with excell xls files. '''
    
        
    def __init__(self, xls_file_name=None):
        '''Class constructor

        Keyword arguments:
        xls_file_name -- filename to work on (default None)
                
        '''
        
        self.book_name = xls_file_name
        self.error_message = ' '
        pass

    def check_name_has_extention(self, book_name=None):
        '''Function to check if the book name has the extention

        Keyword arguments:
        book_name -- filename to work check (default None)

        Returns:
        True - if is file has the right time of extention
        False - if the file extention is not the expected
        
        '''
        
        if(book_name==None):
            return False
        elif(book_name.find(".xls")>=1):
            return True
        pass
    
    def open_book(self):
        '''Function to open the book file

        Returns:
        False - if the file couldn't be open due to wrong extention or is absent
        
        '''
        
        if(self.check_name_has_extention(self.book_name)):
            try:
                self.book = xlrd.open_workbook(self.book_name)
                pass
            except:
                self.error_message = 'Book name passed with extention but book wasn\'t not found.'
                return False
        else:
            try:
                self.book = xlrd.open_workbook(self.book_name + '.xls')
                pass
            except:
                self.error_message = 'Book wasn\'t not found.'
                return False
            pass
        pass
    
    def get_last_from_list(self, item_list):
        '''Function to last item from a list

        Returns:
        False - if the list to get item from isnt passed
        
        '''
        if(item_list == None):
            return False
        i = len(item_list) - 1
        keep_tracking_last_item = True
        while(keep_tracking_last_item):
            if(item_list[i][0]):
               return item_list[i][1]
            i = i-1
        pass
    
    def set_range(self):
        '''Function to set the number of lines and columns to work with

        Returns:
        False - if the list to get item from isnt passed
        
        '''
        sheet = self.sheet
               
        for x in xrange(0,sheet.nrows):
            if(sheet.cell_value(x, 0) == 'TOTAL'):
                table_rows = x
                break
        for x in xrange(0,sheet.ncols):
            if(sheet.cell_value(2, x) == '2010/11 (2)'):
                table_cols = x+2
                break
        if(table_rows == 0 or table_cols == 0):
            return False
        
        self.table_rows = table_rows
        self.table_cols = table_cols
        pass
    
    def select_book_sheet(self, sheet_name = None):
        '''Function to open a book sheet

        Keyword arguments:
        sheet_name -- sheet name to open/return (default None)

        Returns
        False - if couldn't find table boundaries
        
        '''
        
        if(sheet_name == None):
            self.error_message = 'Sheet name missing.'
            return False
        book = self.book
        try:
           self.sheet = book.sheet_by_name(sheet_name)
        except:
            self.error_message = 'Sheet doesn\'t exist.'
            return False

    pass

    def get_lists(self):
        '''Function to get the lists


        '''
        sheet = self.sheet
        
        school = []
        school_number = 0
        faculty = []
        faculty_number = 0
        degree = []
        degree_number = 0
        course = []
        course_number = 0

        self.set_range()
        
        table_rows = self.table_rows
        table_cols = self.table_cols

        for x in xrange(4,20):
            for y in xrange(0,table_cols):
                cell = sheet.cell_value(x,y)
                if(y == 0):
                    school.append((cell,school_number))
                    school_number +=1
                elif(y == 1):
                    faculty.append((cell,faculty_number,self.get_last_from_list(school)))
                    faculty_number +=1
                elif(y == 2):
                    degree.append((cell,degree_number,self.get_last_from_list(faculty)))
                    degree_number +=1
                elif(y == 3):
                    course.append((cell,course_number,self.get_last_from_list(degree)))
                    course_number +=1
        self.course = course
        self.degree = degree
        self.faculty = faculty
        self.school = school
    pass
pass


